// pages/chooseStore/chooseStore.js
import { URL, SER_KEY } from "../../config.js"
import { POST, GET } from "../../utils/request.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    longitude:'',//经度
    latitude:'',//纬度
    defaultCity:'',
    storeList:[],
    logUrl:'',
    searchName:'',
    choose_lat:'',
    choose_lng:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('ee')
    this.GPSsubmit()
    
  },
  GPSsubmit: function (e) {
    let that=this
    wx.getLocation({
      type: 'wgs84',
      success: (res) => {
        console.log(res)
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
        that.setData({ latitude: latitude, longitude: longitude })
        that.getAppid()
      }

    })
  },
  chooseCity:function(){
    let that=this
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          defaultCity: res.address,
          choose_lat: res.latitude,
          choose_lng: res.longitude
        }, () => that.getStoreList(1))
        
        that.setData({
          roomname: res.name
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
   
  },
  getAppid:function(){
    let that=this
    let extConfig = wx.getExtConfigSync? wx.getExtConfigSync(): {}
    console.log(extConfig)
    if(extConfig.appId){
      getApp().globalData.appId = extConfig.appId
      that.getStoreList()
    }else {
      wx.showModal({
        title: '提示',
        content: "微信版本太低,无法使用小程序，请及时升级您的微信。",
        showCancel: false,
        success: function (res) {
        }
      })
    }
    // if (wx.getExtConfig) {
    //   wx.getExtConfig({
    //     success: function (res) {
    //       //给全局变量appId赋值
    //       console.log(res)
    //       getApp().globalData.appId = res.extConfig.appId
    //       that.getStoreList()
    //     },
    //     fail: function (res) {
    //      console.log(res)
    //     }
    //   })
    // } else {
    //   wx.showModal({
    //     title: '提示',
    //     content: "微信版本太低,无法使用小程序，请及时升级您的微信。",
    //     showCancel: false,
    //     success: function (res) {
    //     }
    //   })
    // }
  },
  getStoreList:function(type){
    let that = this;
    let url = URL.HTTP_BASE_INTRANET + SER_KEY.SER_KEY1;
    let data = {
      appid: getApp().globalData.appId,
      lat: that.data.latitude,
      lng: that.data.longitude,
      choose_lat: that.data.choose_lat,
      choose_lng: that.data.choose_lng
    };
    GET(url, data, (ret) => {
      if (ret.statusCode == 200) {
        wx.stopPullDownRefresh()
        if(type==1){
          that.setData({
            logUrl: ret.data.merchant_logo,
            storeList: ret.data.shopList
          }, () => that.data.logUrl)
        }else{
          that.setData({
            defaultCity: ret.data.currentCity,
            storeList: ret.data.shopList,
            logUrl: ret.data.merchant_logo
          })
        }
      } else {
        wx.showToast({
          title: '数据走丢啦，请稍后再试',
          icon: 'none'
        });
      }
    })
  },
  searchStoreName:function(e){
    this.setData({
      searchName: e.detail.value
    })

  },
  placeOrder:function(e){
    let sessionId = wx.getStorageSync('session_id')
    const {item,id}  = e.currentTarget.dataset
    wx.setStorageSync('id', id)
    wx.setStorageSync('shopData', JSON.stringify(item))
    if (sessionId && id){
      wx.switchTab({
        url: '../index/index'
      })
    }else{
      wx.navigateTo({
        url: '../authorized-login/authorized-login',
      })
    }
    
  },
  search:function(){
    let that=this
    var list = that.data.storeList
    var name = that.data.searchName
    console.log(name)
    var newList=[]
    list.map(item=>{
      if (name&&item.shop_name.indexOf(name)!=-1){

          newList.push(item)
      } else if (name==''){
        that.getStoreList()
      }
    })
    
    that.setData({
      storeList: newList
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getStoreList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})